package com.inspur.edp.common.expr.visitor.construct;


import com.inspur.edp.common.expr.exception.ExpressException;
import com.inspur.edp.common.expr.spi.ConstructVisitor;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author lizhaorui
 * @description TODO
 * @date 2024/9/27 13:56
 */
public class JavaConstructFactory {

    private final static Object lockObject = new Object();

    private static Map<String, ConstructVisitor> PROVIDER_MAP;

    public static ConstructVisitor getSpi(String type) {
        if (PROVIDER_MAP == null) {
            synchronized (lockObject) {
                if (PROVIDER_MAP == null) {
                    ConcurrentHashMap<String, ConstructVisitor> temp = new ConcurrentHashMap<>();
                    //固定的几个
                    temp.put(JavaDefaultConstructVisitor.MODE, new JavaDefaultConstructVisitor());
                    temp.put(JavaJsonArrayConstructVisitor.MODE, new JavaJsonArrayConstructVisitor());
                    temp.put(JavaJsonObjectConstructVisitor.MODE, new JavaJsonObjectConstructVisitor());
                    temp.put(JavaSpringBeanConstructVisitor.MODE, new JavaSpringBeanConstructVisitor());
                    temp.put(JavaArrayConstructVisitor.MODE, new JavaArrayConstructVisitor());
                    temp.put(ArrayListConstructorVisitor.MODE, new ArrayListConstructorVisitor());
                    temp.put(NumberTypeConstructorVisitor.MODE, new NumberTypeConstructorVisitor());
                    temp.put(BooleanConstructVisitor.MODE, new BooleanConstructVisitor());
                    PROVIDER_MAP = temp;
                }
            }
        }
        ConstructVisitor genSpi = PROVIDER_MAP.get(type);
        if (genSpi != null) {
            return genSpi;
        } else {
            throw new ExpressException("not find field construct provider " + type);
        }
    }

}
